/* -*-c++-*- VirtualPlanetBuilder - Copyright (C) 1998-2007 Robert Osfield
 *
 * This library is open source and may be redistributed and/or modified under
 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
 * (at your option) any later version.  The full license is in LICENSE file
 * included with this distribution, and on the openscenegraph.org website.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * OpenSceneGraph Public License for more details.
*/

#ifndef GEOSPATIALDATASET_H
#define GEOSPATIALDATASET_H 1

#include <osg/Timer>
#include <osgDB/FileUtils>

#include <OpenThreads/Mutex>

#include <vpb/Export>

#include <gdal_priv.h>

namespace vpb
{

enum AccessMode
{
    READ_ONLY,
    READ_AND_WRITE
};

class VPB_EXPORT GeospatialDataset : public osg::Referenced
{
    public:
    
        GeospatialDataset(const std::string& filename, AccessMode accessMode);
        GeospatialDataset(GDALDataset* dataset);

        CPLErr GetGeoTransform( double * ptr);
        
        int         GetRasterXSize( void );
        int         GetRasterYSize( void );
        int         GetRasterCount( void );
        GDALRasterBand *GetRasterBand( int band );

        int GetOverviewCount( int band );

        bool containsOverviews();

        int    GetGCPCount();
        const char *GetGCPProjection();
        const GDAL_GCP *GetGCPs();
        
        const char *GetProjectionRef(void);

        CPLErr      RasterIO( GDALRWFlag a, int b, int c, int d, int e,
                              void * f, int g, int h, GDALDataType i,
                              int j, int * k, int l, int m, int n);

        CPLErr BuildOverviews( const char * a, int b, int * c,
                               int d, int * e, GDALProgressFunc f, void * g);

        GDALDataset* getGDALDataset() { updateTimeStamp(); return _dataset; }
        
        void updateTimeStamp() { _timeStamp = osg::Timer::instance()->time_s(); }
        
        double getTimeStamp() const { return _timeStamp; }
        
        OpenThreads::Mutex& getMutex() const { return _mutex; }
        
    protected:

        virtual ~GeospatialDataset();
        
        mutable OpenThreads::Mutex  _mutex;
        GDALDataset*                _dataset;
        double                      _timeStamp;
};

}

#endif
